package com.bwie.controller;

import com.bwie.common.domain.Order;
import com.bwie.common.domain.ResourceInfo;
import com.bwie.common.domain.request.OrderListReq;
import com.bwie.common.domain.request.ResourceInfoListReq;
import com.bwie.common.result.PageResult;
import com.bwie.common.result.Result;
import com.bwie.service.ResourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @className: ResourceController
 * @Description: TODO
 * @version: v1.８.0
 * @author: 汪菲阳
 * @date: 2025/11/10 11:10
 */
@CrossOrigin
@RestController
@RequestMapping("resource")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    /**
     * 资源列表
     * @param resourceInfoListReq
     * @return
     */
    @PostMapping("infoList")
    public Result<PageResult<ResourceInfo>> infoList(@RequestBody ResourceInfoListReq resourceInfoListReq){
        PageHelper.startPage(resourceInfoListReq.getPageNum(),resourceInfoListReq.getPageSize());
        //查询数据
        List<ResourceInfo> infoList=resourceService.infoList(resourceInfoListReq);
        PageInfo<ResourceInfo> pageInfo = new PageInfo<>(infoList);
        //返回
        return PageResult.toResult(pageInfo.getTotal(), infoList);
    }

    /**
     * 导入
     * @return
     */
    @PostMapping("importFile")
    public Result<String> importFile(MultipartFile file){
        String taskId = resourceService.importFile(file);
        return Result.success(taskId);
    }

    /**
     * 预览
     * @return
     */
    @PostMapping("previewList")
    public Result<PageResult<ResourceInfo>> previewList(@RequestBody ResourceInfoListReq resourceInfoListReq){
        PageHelper.startPage(resourceInfoListReq.getPageNum(),resourceInfoListReq.getPageSize());
        //查询数据
        List<ResourceInfo> previewList=resourceService.previewList(resourceInfoListReq);
        PageInfo<ResourceInfo> pageInfo = new PageInfo<>(previewList);
        return PageResult.toResult(pageInfo.getTotal(),previewList);
    }

    /**
     * 开始异步查重
     * @param taskId 任务ID
     * @return
     */
    @PostMapping("startDuplicateCheck/{taskId}")
    public Result<String> startDuplicateCheck(@PathVariable String taskId){
        resourceService.startDuplicateCheck(taskId);
        return Result.success("开始查重");
    }

    /**
     * 获取查重进度
     */
    @GetMapping("getDuplicateProgress/{taskId}")
    public Result<Map<String, Object>> getDuplicateProgress(@PathVariable String taskId){
        Map<String, Object> progress = resourceService.getDuplicateProgress(taskId);
        return Result.success(progress);
    }

    /**
     * 获取查重结果
     */
    @GetMapping("getDuplicateResult/{taskId}")
    public Result<Map<String, Object>> getDuplicateResult(@PathVariable String taskId){
        Map<String, Object> result = resourceService.getDuplicateResult(taskId);
        return Result.success(result);
    }

    /**
     * 批量保存
     */
    @PostMapping("batchSave/{taskId}")
    public Result<String> batchSave(@PathVariable String taskId){
        resourceService.batchSave(taskId);
        return Result.success("保存成功");
    }

    /**
     * 取消导入
     */
    @PostMapping("cancelImport/{taskId}")
    public Result<String> cancelImport(@PathVariable String taskId){
        resourceService.cancelImport(taskId);
        return Result.success("取消成功");
    }

    /**
     * 订单列表---详情
     * @return
     */
    @PostMapping("orderList")
    public Result<PageResult<Order>> orderList(@RequestBody OrderListReq orderListReq){
        PageHelper.startPage(orderListReq.getPageNum(),orderListReq.getPageSize());
        List<Order> orderList=resourceService.orderList(orderListReq);
        PageInfo<Order> orderPageInfo = new PageInfo<>(orderList);
        return PageResult.toResult(orderPageInfo.getTotal(),orderList);
    }
}